use super::item::Item;
use common_tree::Node;

pub struct GroupData {
    name: String,
    items: Vec<Item>,
}

impl GroupData {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_owned(),
            items: vec![],
        }
    }

    pub fn name(&self) -> &String {
        &self.name
    }

    pub fn items(&self) -> &Vec<Item> {
        &self.items
    }

    pub fn add_item(&mut self, item: Item) {
        self.items.push(item);
    }
}

pub struct Group {
    root: Node<GroupData>,
}

impl Group {
    pub fn new(name: &str) -> Self {
        let group_data = GroupData::new(name);
        Group {
            root: Node::new(group_data),
        }
    }

    pub fn root(&self) -> &Node<GroupData> {
        &self.root
    }

    ///
    ///
    pub fn add_node_by_level(&mut self, level: usize, group_data: GroupData) {
        let node_opt = self.root.last_child_mut_by_level(level);
        let new_node = Node::new(group_data);
        match node_opt {
            Some(node) => node.add(new_node),
            None => (),
        }
    }

    pub fn add_item_by_level(&mut self, level: usize, item: Item) {
        let node_opt = self.root.last_child_mut_by_level(level);
        match node_opt {
            Some(node) => node.data_mut().add_item(item),
            None => (),
        }
    }
}
